//
//  KWNFinishViewController.m
//  KWNClient
//
//  Created by 张明磊 on 14-10-16.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KWNFinishViewController.h"
#import "Model_question.h"
#import "Model_user.h"
#import "KWNNetwork.h"
//#import "KWNQuestionFinishViewController.h"
//#import "KWNLoginViewController.h"

typedef NS_ENUM(NSInteger, KWNFinishButtonTag){
    KWNFinishButtonTag_back          = 0,
    KWNFinishButtonTag_finish              ,
    KWNFinishButtonTag_mima              ,
};

@interface KWNFinishViewController ()

@property (weak, nonatomic) UITextField *textField_MM;
#pragma mark - tag标签 -
/**
 0 - 注册后设置密码
 1 - 找回密码设置
 */
@property (assign, nonatomic) NSInteger int_tag;

@end

@implementation KWNFinishViewController

#pragma mark - 设置密码 -
/**
 0 - 注册后设置
 1 - 找回密码设置
 */
- (id)initWithTag:(NSInteger )tag{
    self = [super init];
    if(self){
        _int_tag = tag;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self UI_finish];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)UI_finish{
    UIView *imageView_title = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    imageView_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:imageView_title];
    
    UIButton *button_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    button_menu.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_menu setImage:[UIImage imageFileName:@"button_back.png"] forState:UIControlStateNormal];
    [button_menu addTarget:self action:@selector(didClickButton_finish:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_menu];
    button_menu.tag = KWNFinishButtonTag_back;
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(imageView_title.frame.origin.x,IOS7_SIZE + 5,imageView_title.frame.size.width,35)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.textColor = ColorOfTitle;
    label_title.textAlignment = NSTextAlignmentCenter;
    label_title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0f];
    [self.view addSubview:label_title];
    
    UILabel *label_phone = [[UILabel alloc] initWithFrame:CGRectMake(30, imageView_title.frame.origin.y + imageView_title.frame.size.height + 10, 200, 30)];
    label_phone.backgroundColor = [UIColor clearColor];
    label_phone.textColor = RGB(169, 167, 167);
    label_phone.textAlignment = NSTextAlignmentLeft;
    label_phone.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
    [self.view addSubview:label_phone];

    switch (_int_tag) {
        case 0:{
            label_title.text = @"用户注册";
            label_phone.text = @"请设置密码";
        }break;
        case 1:{
            label_title.text = @"重置密码";
            label_phone.text = @"请设置新密码";
        }break;
        case 2:{
            label_title.text = @"用户注册";
            label_phone.text = @"请设置密码";
        }break;
        default:
            break;
    }

    
    UIView *view_phone = [[UIView alloc] initWithFrame:CGRectMake(10, label_phone.frame.origin.y + label_phone.frame.size.height, 300, 46)];
    view_phone.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_phone];
    //设置矩形四个圆角半径
    view_phone.layer.cornerRadius = 5.0;
    view_phone.layer.borderWidth = 1.0;//边框宽度
    view_phone.layer.borderColor = RGB(198, 198, 198).CGColor;//边框颜色
    
    UITextField *textField_phone = [[UITextField alloc] initWithFrame:CGRectMake(30, label_phone.frame.origin.y + label_phone.frame.size.height, 200, 46)];
    textField_phone.backgroundColor = [UIColor clearColor];
    textField_phone.textColor = [UIColor blackColor];
    textField_phone.textAlignment = NSTextAlignmentLeft;
    textField_phone.placeholder = @"6-16个字符，区分大小写";
    textField_phone.font = [UIFont systemFontOfSize:15];
    textField_phone.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField_phone.clearButtonMode = UITextFieldViewModeAlways;
    textField_phone.keyboardType = UIKeyboardTypeEmailAddress;
    //去掉首字母大写和自动纠错
    textField_phone.autocorrectionType = UITextAutocorrectionTypeNo;
    textField_phone.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField_phone.secureTextEntry = YES;
    //textField_phone.delegate = self;
    //字多自动缩进
    textField_phone.adjustsFontSizeToFitWidth = YES;
    //缩进最小字体
    textField_phone.minimumFontSize = 20;
    [self.view addSubview:textField_phone];
//    [textField_phone becomeFirstResponder];
    _textField_MM = textField_phone;
    
    UIButton *button_mima = [UIButton buttonWithType:UIButtonTypeCustom];
    button_mima.frame = CGRectMake(textField_phone.frame.origin.x + textField_phone.frame.size.width + 5, textField_phone.frame.origin.y + 5, 62, 35);
    [button_mima setImage:[UIImage imageFileName:@"button_showM.png"] forState:UIControlStateNormal];
    [button_mima setImage:[UIImage imageFileName:@"button_showM_s.png"] forState:UIControlStateSelected];
    [button_mima addTarget:self action:@selector(didClickButton_finish:)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_mima];
    button_mima.tag = KWNFinishButtonTag_mima;
    
    UIButton *button_send = [UIButton buttonWithType:UIButtonTypeCustom];
    button_send.frame = CGRectMake(view_phone.frame.origin.x, view_phone.frame.origin.y + view_phone.frame.size.height + 10, view_phone.frame.size.width, view_phone.frame.size.height);
    button_send.backgroundColor = RGB(91, 165, 229);
    [button_send setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    switch (_int_tag) {
        case 0:{
           [button_send setTitle:@"完成注册" forState:UIControlStateNormal];
        }break;
        case 1:{
            [button_send setTitle:@"完成重置" forState:UIControlStateNormal];
        }break;
        case 2:{
            [button_send setTitle:@"完成注册" forState:UIControlStateNormal];
        }break;
        default:
            break;
    }

    button_send.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    [button_send addTarget:self action:@selector(didClickButton_finish:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_send];
    button_send.layer.cornerRadius = 5.0;
    button_send.tag = KWNFinishButtonTag_finish;
    
    UIButton *button_closeKey = [UIButton buttonWithType:UIButtonTypeCustom];
    button_closeKey.frame = CGRectMake(0, 0, 320, 30);
    [button_closeKey setImage:[UIImage imageFileName:@"button_closeKey.png"] forState:UIControlStateNormal];
    [button_closeKey addTarget:self action:@selector(clickBtn_finshcloseKeybored)forControlEvents:UIControlEventTouchUpInside];
    [_textField_MM setInputAccessoryView:button_closeKey];
}

- (void)clickBtn_finshcloseKeybored
{
    [self.view endEditing:YES];
}

- (void)didClickButton_finish:(UIButton *)button{
    switch (button.tag) {
        case KWNFinishButtonTag_back:{
            [self popViewController];
        }break;
        case KWNFinishButtonTag_finish:{
            [self clickBtn_finshcloseKeybored];
            [SVProgressHUD showWithStatus:@"注册中..." maskType:SVProgressHUDMaskTypeClear];
            NSString *string_password = _textField_MM.text;
            NSString *string_uid = [Model_question sharedInstance].string_uid;
            if(!string_uid){
                string_uid = [[Model_user sharedInstance] getUserID];
            }
            [[KWNNetwork sharedInstance] regist_setPassword:string_password andUid:string_uid finishBlock:^(NSInteger stateCode, NSMutableArray *result){
                if(stateCode == 10000){
                    //NSString *string_msg  = result[0];
                    NSString *string_id = result[1];
                    NSString *string_token = result[2];
                    [[Model_user sharedInstance] setUID:string_id];
                    [[Model_user sharedInstance] setToken:string_token];
                    [Model_question sharedInstance].string_token = string_token;
                    [[Model_user sharedInstance] setUserPhone:[Model_question sharedInstance].string_phone];
                    if(_int_tag == 0){
                        [SVProgressHUD dismissWithSuccess:@"注册成功!" afterDelay:1.5];
                    }
                    else{
                        if(_int_tag == 1){
                            [SVProgressHUD dismissWithSuccess:@"重置成功!" afterDelay:1.5];
                        }
                        else{
                            [SVProgressHUD dismissWithSuccess:@"注册成功!" afterDelay:1.5];
                        }
                        double delayInSeconds = 1.5;
                        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                            [self.navigationController popToRootViewControllerAnimated:YES];
                        });
                        return ;
                    }
                }
                else{
                    NSString *string_msg  = result[0];
                    [SVProgressHUD dismissWithError:string_msg afterDelay:1.5];
                }
            }];
        }break;
        case KWNFinishButtonTag_mima:{
            button.selected =! button.selected;
            _textField_MM.secureTextEntry =! _textField_MM.secureTextEntry;
            NSString *string_mm = _textField_MM.text;
            _textField_MM.text = nil;
            _textField_MM.text = string_mm;
        }break;
        default:
            break;
    }
}

@end
